package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText Et_AcNum,Et_PassWord;
    private Button Btn_Login,Btn_Register,Btn_ForgetPassWord;
    private String AccountNumber;
    private String PassWord;
    private String date;
    private int close;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Et_AcNum = findViewById(R.id.Et_AcNum);
        Et_PassWord = findViewById(R.id.Et_PassWord);

        Btn_Login = findViewById(R.id.Btn_Login);
        Btn_Register = findViewById(R.id.Btn_Register);
        Btn_ForgetPassWord = findViewById(R.id.Btn_ForgetPassWord);

        Btn_Login.setOnClickListener(this);
        Btn_Register.setOnClickListener(this);
        Btn_ForgetPassWord.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
            switch (v.getId()){
                case R.id.Btn_Login:
                    if (Et_AcNum.getText().toString().equals("")||Et_PassWord.getText().toString().equals("")){
                        Toast.makeText(this, "账号和密码不能为空!", Toast.LENGTH_SHORT).show();
                    }else{
                        login();
                    }

                    break;
                case R.id.Btn_Register:
                    Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
                    startActivity(intent);
                    break;
                case R.id.Btn_ForgetPassWord:
                    Intent intent1 = new Intent(MainActivity.this,ForgetPassWordActivity.class);
                    startActivity(intent1);
                    break;
            }
    }

    private void login() {
        
        new Thread(new Runnable() {
            private static final String TAG = "MainActivity";
            @Override
            public void run() {
                //1 client对象
                OkHttpClient client = new OkHttpClient();
                //2 Request对象
                FormBody body = new FormBody.Builder()

                        .build();
                Request request = new Request.Builder()
                        .url("http://10.2.61.86:8080/juyuan/users/list?accountNumber="+Et_AcNum.getText().toString() )
                        .post(body)
                        .build();
                //3 call对象
                final Call call = client.newCall(request);
                //4 执行call对象得到数据
                try {
                    Response response = call.execute();
                    final String data = response.body().string();

                    date = data;

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(date.equals("null")) {
                                Toast.makeText(MainActivity.this, "用户不存在或密码输入错误！", Toast.LENGTH_SHORT).show();
                                //判断用户是否存在 存在=1；不存在=0；
                                close = 0;
                            }else if(!date.equals("null")){
                                close = 1;
                            }
                        }
                    });
                    Log.e(TAG, "close: "+close );
                    if (close == 1){
                        Gson gson = new Gson();
                        LoginGson gu = gson.fromJson(data, LoginGson.class);
                        AccountNumber = gu.getAccountNumber();

                        PassWord = gu.getPassWord();

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                if (Et_AcNum.getText().toString().equals(AccountNumber)&&
                                        Et_PassWord.getText().toString().equals(PassWord)&&close==1){

                                    Toast.makeText(MainActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(MainActivity.this,FoodListActivity.class);
                                    startActivity(intent);
                                }
                            }
                        });
                    }



                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }).start();

    }
}